const app = getApp()
export function login(){
  wx.login({
    success (res) {
      if (res.code) {
        console.log(res.code)
        requestHttp('/app/auth/wechat/login', {code: res.code}, 'get').then(({data}) => {
          app.globalData.token = 'Bearer '+data.token
          app.globalData.type = data.type
          console.log(data.type)
          console.log(data.token)
        })  
      }
    }
  })    
}

function requestHttp(url,data,method){
  return new Promise(function (reslove, reject) {
    console.log(app.globalData.token)
    let headers = {
      'Authorization': app.globalData.token || ''
    };
    wx.request({
      url: app.globalData.host + url,
      method: method || 'get',
      data: data,
      header: headers,
      success: function success(res) {
        const data = res.data;
        if (data.code * 1 === 0) {
          reslove(data)
        } else {
          wx.showToast({
            title: data.msg || '网络异常',
            icon: 'error',
          })
          setTimeout(() => {
            reject(data);
          }, 10)
        }
        // reslove(res)
      },
      fail: function fail(res) {
        wx.showToast({
          title: res,
          icon: 'error',
        })
        reject(res);
      }
    })
  })
}